//
//  XJBottomTabBar.swift
//  XJWeiBO
//
//  Created by 452608533 on 16/7/19.
//  Copyright © 2016年 452608533. All rights reserved.
//

import UIKit

protocol XJBottomTabBarDelegate:NSObjectProtocol{
    
    func didClickComposeButton()
}

class XJBottomTabBar: UITabBar {
    
   weak var xjBottomDelegate :XJBottomTabBarDelegate?
    
    var closure:(()->())?

   override init(frame: CGRect) {
        super.init(frame: frame)
    
    setupUI()
    }
    
    func setupUI() {

        addSubview(composeButotn)
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let w = frame.width * 0.2
        var index: CGFloat = 0
        
        for value in subviews{
            
            if value.isKindOfClass(NSClassFromString("UITabBarButton")!) {
                value.frame.origin.x = index * w
                value.frame.size.width = w
                
                index++
                
                if index == 2 {
                    index++
                }
            }
        }
        composeButotn.center.x = frame.width * 0.5
        composeButotn.center.y = frame.height * 0.5
    }

   required init?(coder aDecoder: NSCoder) {
       fatalError("init(coder:) has not been implemented")
   }
    
    //撰写按钮的监听方法
    
    func clickComposeButton() {
        //通知代理完成
//        xjBottomDelegate?.didClickComposeButton()
        closure?()
    }


///  懒加载控件
    lazy var composeButotn: UIButton = {
       
        let button = UIButton()
        button.addTarget(self, action: "clickComposeButton", forControlEvents: UIControlEvents.TouchUpInside)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.sizeToFit()
        return button
    }()
    
}
